/// 延迟执行任务，任务执行完成后 ，可能需要执行回调onFinish
class FFDelay2<T> {
  String id;
  Duration? delay;
  bool isDispose = false;
  Future<T> Function(FFDelay2? delay) onDelay;
  Function(FFDelay2? delay, T onDelayResult, String id) onFinish;

  FFDelay2({
    required this.id,
    required this.onDelay,
    required this.onFinish,
    this.delay,
  });

  ///在延时多久后执行
  start() async {
    if (delay != null) {
      await Future.delayed(delay!);
    }
    if (isDispose) return;
    T r = await onDelay(this);
    if (isDispose) return;
    onFinish(this, r, id);
  }

  ///标记不再执行
  dispose() {
    isDispose = true;
  }
}
